package com.fancy.learncenter.ui.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.CartoonAnswerKeysBean;
import com.fancy.learncenter.bean.CartoonFillBlankBean;
import com.fancy.learncenter.bean.CartoonMediaBean;
import com.fancy.learncenter.bean.CartoonOptionsBean;
import com.fancy.learncenter.bean.CartoonStemsBean;
import com.fancy.learncenter.bean.CartoonTextsBean;
import com.fancy.learncenter.bean.QuestionVOsBean;
import com.fancy.learncenter.common.Constant;
import com.fancy.learncenter.common.JumpIntentKey;
import com.fancy.learncenter.common.MAudioPlayer;
import com.fancy.learncenter.ui.adapter.base.CommonRecycleViewAdapter;
import com.fancy.learncenter.ui.adapter.base.CustomViewHold;
import com.fancy.learncenter.ui.callback.AlbumImpl;
import com.fancy.learncenter.ui.fragment.GrammarChooseFillBlankNewFragment;
import com.fancy.learncenter.ui.fragment.WordFillBlankNewFragment;
import com.fancy.learncenter.ui.view.GridSpacingItemDecoration;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.utils.TeacherWorkPreviewDataUtil;
import com.fancy.learncenter.utils.Utils;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

import static com.fancy.learncenter.common.JumpIntentKey.CARTOON_BUTTON_STATUS.STATUS_ANALYSIS;
import static com.fancy.learncenter.common.JumpIntentKey.OPTION_STATUS.STATUS_DEFAULT;

/**
 * Created by shiweiwei on 2018/1/25.
 */

public class PreviewHomeworkAdapter extends CommonRecycleViewAdapter<QuestionVOsBean> {


    private List<QuestionVOsBean> itemDatas;

    public static final int QUESTION_TYPE_1 = 1;
    public static final int QUESTION_TYPE_2 = 2;
    public static final int QUESTION_TYPE_3 = 3;
    public static final int QUESTION_TYPE_4 = 4;
    public static final int QUESTION_TYPE_5 = 5;
    public static final int QUESTION_TYPE_6 = 6;
    public static final int QUESTION_TYPE_7 = 7;
    public static final int QUESTION_TYPE_8 = 8;
    public static final int QUESTION_TYPE_9 = 9;

    public MAudioPlayer getAudioPlayer() {
        return audioPlayer;
    }

    public void setAudioPlayer(MAudioPlayer audioPlayer) {
        this.audioPlayer = audioPlayer;
    }

    MAudioPlayer audioPlayer = null;

    public ImageView getVoice_image() {
        return voice_image;
    }

    ImageView voice_image;

    @Override
    public int getAdapterItemViewType(int position) {
        switch (itemDatas.get(position).getAppPageKey()) {
            case Constant.LCTP://听音选图
                CartoonOptionsBean optionsBeanListen = itemDatas.get(position).getOptions();
                if (optionsBeanListen != null) {
                    List<CartoonMediaBean> imageList = optionsBeanListen.getImages();
                    if (imageList != null && imageList.size() == 2) {
                        return QUESTION_TYPE_1;
                    } else {
                        return QUESTION_TYPE_2;
                    }
                }
            case Constant.LPTPT://看词拼图
                return QUESTION_TYPE_3;
            case Constant.LCTW://看图选词：
                return QUESTION_TYPE_4;
            case Constant.LLAS://听音拼写
                return QUESTION_TYPE_5;
            case Constant.USFB://选词填空
                return QUESTION_TYPE_6;
            case Constant.LAC://听音选择
                return QUESTION_TYPE_7;
            case Constant.LPC://看图选择
                return QUESTION_TYPE_8;
            case Constant.FTB://填空
                return QUESTION_TYPE_9;

        }
        return 0;
    }

    public PreviewHomeworkAdapter(Context mContext, List<QuestionVOsBean> itemDatas) {
        super(mContext, R.layout.empty_view, itemDatas);
        this.itemDatas = itemDatas;
    }

    @Override
    public CustomViewHold onCreateViewHolder(ViewGroup parent, int viewType) {
        CustomViewHold viewHold = null;
        switch (viewType) {
            case QUESTION_TYPE_1:
                viewHold = CustomViewHold.creatViewHolder(parent.getContext(), parent, R.layout.teacher_see_image_2choose_layout);
                break;
            case QUESTION_TYPE_2:
                viewHold = CustomViewHold.creatViewHolder(parent.getContext(), parent, R.layout.teacher_see_image_4choose_layout);
                break;
            case QUESTION_TYPE_3:
                viewHold = CustomViewHold.creatViewHolder(parent.getContext(), parent, R.layout.teacher_see_puzzle_layout);
                break;
            case QUESTION_TYPE_4:
                viewHold = CustomViewHold.creatViewHolder(parent.getContext(), parent, R.layout.teacher_see_image_choose_word_layout);
                break;
            case QUESTION_TYPE_5:
                viewHold = CustomViewHold.creatViewHolder(parent.getContext(), parent, R.layout.teacher_listen_spell_layout);
                break;
            case QUESTION_TYPE_6:
                viewHold = CustomViewHold.creatViewHolder(parent.getContext(), parent, R.layout.teacher_choose_fill_blank_layout);
                break;
            case QUESTION_TYPE_7:
                viewHold = CustomViewHold.creatViewHolder(parent.getContext(), parent, R.layout.teacher_listen_and_choose);
                break;
            case QUESTION_TYPE_8:
                viewHold = CustomViewHold.creatViewHolder(parent.getContext(), parent, R.layout.teacher_see_image_choose_layout);
                break;
            case QUESTION_TYPE_9:
                viewHold = CustomViewHold.creatViewHolder(parent.getContext(), parent, R.layout.teacher_fill_blank_layout);
                break;
        }
        return viewHold;
    }

    @Override
    public void bindView(CustomViewHold customViewHold, QuestionVOsBean item, int position) {
        TextView question_name;
        RecyclerView recyclerView = null;
        TextView see_answer_layout;
        SimpleDraweeView question_image = null;
        final JumpIntentKey.QUESTION_ENTER_TYPE type;
        see_answer_layout = customViewHold.getView(R.id.see_answer_layout);
        see_answer_layout.setVisibility(View.GONE);

        switch (getAdapterItemViewType(position)) {

            case QUESTION_TYPE_1://听音选图
            case QUESTION_TYPE_2://听音选图
                voice_image = customViewHold.getView(R.id.voice_image);
                question_name = customViewHold.getView(R.id.question_name);
                recyclerView = customViewHold.getView(R.id.recyclerView);
                TeacherWorkPreviewDataUtil.LCTP_DataUtils(PreviewHomeworkAdapter.this, mContext, item, question_name, voice_image, recyclerView, see_answer_layout);
                break;
            case QUESTION_TYPE_3://看词拼图
                question_name = customViewHold.getView(R.id.question_name);
                GridView gridView = customViewHold.getView(R.id.drag_grid_view);
                TeacherWorkPreviewDataUtil.LPTPT_DataUtils(mContext, item, question_name, gridView, see_answer_layout);
                break;
            case QUESTION_TYPE_4://看图选词：
                question_image = customViewHold.getView(R.id.question_image);
                recyclerView = customViewHold.getView(R.id.recyclerView);
                TeacherWorkPreviewDataUtil.LCTW_DataUtils(mContext, item, question_image, see_answer_layout, recyclerView);
                break;
            case QUESTION_TYPE_5://听音拼写
                voice_image = customViewHold.getView(R.id.voice_image);
                question_name = customViewHold.getView(R.id.question_name);
                question_image = customViewHold.getView(R.id.question_image);
                TagFlowLayout flowLayout = customViewHold.getView(R.id.flowlayout);
                TeacherWorkPreviewDataUtil.LLAS_DataUtils(PreviewHomeworkAdapter.this, mContext, item, voice_image, question_image, flowLayout, see_answer_layout);
                break;
            case QUESTION_TYPE_6://选词填空

                TagFlowLayout flowlayout = customViewHold.getView(R.id.flowlayout);
                List<CartoonFillBlankBean> blankList;
                blankList = new ArrayList<>();

                CartoonStemsBean stemsBean = item.getStems();
                String questionStr = stemsBean.getTexts().get(0).getValue();


                String[] questionNameList = questionStr.split(" ");
                for (int i = 0; i < questionNameList.length; i++) {

                    CartoonFillBlankBean blankBean = new CartoonFillBlankBean();
                    blankBean.setStatus(STATUS_DEFAULT);
                    blankBean.setValue(questionNameList[i]);
                    CartoonFillBlankBean blankBean2 = new CartoonFillBlankBean();
                    blankBean2.setStatus(STATUS_DEFAULT);
                    blankBean2.setValue(" ");
                    blankList.add(blankBean);
                    blankList.add(blankBean2);
                }

                FillBlanAdapter fillBlankAdapter = new FillBlanAdapter(mContext, blankList);
                flowlayout.setAdapter(fillBlankAdapter);


                recyclerView = customViewHold.getView(R.id.recyclerView);

                GridLayoutManager manager = new GridLayoutManager(mContext, 2);
                manager.setOrientation(LinearLayoutManager.VERTICAL);
                recyclerView.setLayoutManager(manager);
                EnglishSentenceAdapter englishSentenceAdapter = new EnglishSentenceAdapter(mContext, item.getOptions().getTexts(), JumpIntentKey.QUESTION_ENTER_TYPE.TEACHER_WORK_PREVIEW);
                recyclerView.setAdapter(englishSentenceAdapter);
                break;
            case QUESTION_TYPE_7://听音选择

                recyclerView = customViewHold.getView(R.id.recyclerView);
                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
                linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                recyclerView.setLayoutManager(linearLayoutManager);

                List<CartoonTextsBean> optionList = new ArrayList<>();
                CartoonOptionsBean optionsBean = item.getOptions();
                optionList.clear();
                optionList.addAll(optionsBean.getTexts());

                CartoonQuestionTextOptionAdapter adapter = new CartoonQuestionTextOptionAdapter(mContext, JumpIntentKey.QUESTION_ENTER_TYPE.TEACHER_WORK_PREVIEW, optionList);
                recyclerView.setAdapter(adapter);

                break;
            case QUESTION_TYPE_8://看图选择

                recyclerView = customViewHold.getView(R.id.recyclerView);
                question_image = customViewHold.getView(R.id.image);

                question_image.setImageURI(item.getOptions().getImages().get(0).getImgUrl());
                List<CartoonTextsBean> optionLists = new ArrayList<>();


                LinearLayoutManager managers = new LinearLayoutManager(mContext);
                managers.setOrientation(LinearLayoutManager.VERTICAL);
                adapter = new CartoonQuestionTextOptionAdapter(mContext, JumpIntentKey.QUESTION_ENTER_TYPE.TEACHER_WORK_PREVIEW, optionLists);
                recyclerView.setLayoutManager(managers);
                recyclerView.setAdapter(adapter);

                break;
            case QUESTION_TYPE_9://填空

                TagFlowLayout flowlayouts = customViewHold.getView(R.id.flowlayout);
                List<CartoonFillBlankBean> blankLists;
                blankList = new ArrayList<>();

                CartoonStemsBean stemsBeans = item.getStems();
                String questionStrs = stemsBeans.getTexts().get(0).getValue();

                if (stemsBeans.getTexts().size() > 1) {
                    LogUtil.MyLog("getQuestionId", "====nextQuestionTv===" + item.getQuestionId());
                }

                String[] questionNameLists = questionStrs.split(" ");
                for (int i = 0; i < questionNameLists.length; i++) {

                    CartoonFillBlankBean blankBean = new CartoonFillBlankBean();
                    blankBean.setStatus(STATUS_DEFAULT);
                    blankBean.setValue(questionNameLists[i]);
                    CartoonFillBlankBean blankBean2 = new CartoonFillBlankBean();
                    blankBean2.setStatus(STATUS_DEFAULT);
                    blankBean2.setValue(" ");
                    blankList.add(blankBean);
                    blankList.add(blankBean2);
                }

                FillBlanAdapter fillBlankAdapters = new FillBlanAdapter(mContext, blankList);
                flowlayouts.setAdapter(fillBlankAdapters);

                break;
        }
    }


}
